package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    //查询所有项
    @Override
    public List<CheckItem> getAllIteam() {
        List<CheckItem> allIteam = checkItemDao.getAllIteam();
        return allIteam;
    }

    //根据ID查询项
    @Override
    public CheckItem findItemById(Integer id) {
        CheckItem itemById = checkItemDao.findItemById(id);
        return itemById;
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //当查询条件不为空时且不为“”，设置页码=1
//        if (queryPageBean.getQueryString() != null) {
//            String strip = queryPageBean.getQueryString().strip();
//            queryPageBean.setQueryString(strip);
//
//            queryPageBean.setCurrentPage(1);
//
//        }

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckItem> items = checkItemDao.findPage(queryPageBean);
        Page<CheckItem> page = (Page<CheckItem>) items;

        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增项
    @Override
    public Integer addItem(CheckItem checkItem) {
        Integer rows = checkItemDao.addItem(checkItem);
        return rows;
    }

    //修改项
    @Override
    public Integer updateItem(CheckItem checkItem) {
        Integer rows = checkItemDao.updateItem(checkItem);
        return rows;
    }

    //删除项
    @Override
    public Integer deleteById(Integer id) {
        Integer rows = checkItemDao.deleteById(id);
        return rows;
    }


}
